using System;
using System.Collections.Generic;
using System.Linq;
using LaxCraft.Domain;
using NHibernate.Linq;

namespace LaxCraft.Repositories
{
	using NHibernate;

	using LaxCraft.Plumbing;

	public class TeamRepository : ITeamRepository
	{
		private readonly ISession session;

		public TeamRepository(ISession session)
		{
			this.session = session;
		}

	    public IQueryable<Stat> GetStats()
	    {
            using(var tx = session.BeginTransaction())
            {
                var data = session.Query<Stat>();
                tx.Commit();
                return data;
            }
	    }

	    public IQueryable<Player> GetPlayers()
        {
            using (var tx = session.BeginTransaction()) {
                var data = session.Query<Player>();
                tx.Commit();
                return data;
            }
        }

	    public IQueryable<XPEntry> GetXPEntries()
	    {
            using (var tx = session.BeginTransaction()) {
                var data = session.Query<XPEntry>();
                tx.Commit();
                return data;
            }
	    }

	    public Player GetPlayer(string name)
	    {
            using (var tx = session.BeginTransaction()) {
                var data = session.QueryOver<Player>().Where(p => p.Name == name).SingleOrDefault();
                tx.Commit();
                return data;
            }
	    }

	    public Player GetPlayer(int id)
	    {
            using (var tx = session.BeginTransaction()) {
                var data = session.QueryOver<Player>().Where(p => p.Id == id).SingleOrDefault();
                tx.Commit();
                return data;
            }
	    }

	    public void SaveXPEntry(XPEntry entry)
	    {
            using (var txn = session.BeginTransaction()) {
                session.Save(entry);
                txn.Commit();
            }
	    }

	    public void SavePlayer(Player player)
	    {
            using (var txn = session.BeginTransaction()) {
                session.Save(player);
                txn.Commit();
            }
	    }
	}
}